package com.glory.hikvisionmobile.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.glory.hikvisionmobile.R;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/1/8 13:51
 * @DESC *****************************
 */
public class HikLabelEditText extends LinearLayout {

    private boolean notnull;
    private String labelStr;
    private float labelSize;
    private int labelColor;
    private float labelContextSize;
    private int labelContextColor;
    private int labelGravity;
    private int labelWeight;
    private int labelContentWeight;
    private String labelContentHint;
    private int labelContentHintColor;

    private TextView label;
    private EditText labelContext;

    public HikLabelEditText(Context context) {
        super(context);
        init(null, 0);
    }

    public HikLabelEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public HikLabelEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyle) {
        if (attrs == null) {
            throw new IllegalArgumentException("must be init by xml");
        }
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.HikLabelEditText, defStyle, 0);
        notnull = array.getBoolean(R.styleable.HikLabelEditText_notnull, false);
        labelStr = array.getString(R.styleable.HikLabelEditText_label);
        labelSize = array.getDimension(R.styleable.HikLabelEditText_labelSize, 24);
        labelColor = array.getColor(R.styleable.HikLabelEditText_labelColor, Color.parseColor("#000000"));
        labelContextSize = array.getDimension(R.styleable.HikLabelEditText_labelContentSize, 24);
        labelContextColor = array.getColor(R.styleable.HikLabelEditText_labelContentColor, Color.parseColor("#000000"));
        labelGravity = array.getInt(R.styleable.HikLabelEditText_labelGravity, 0);
        labelWeight = array.getInt(R.styleable.HikLabelEditText_labelWeight, 0);
        labelContentWeight = array.getInt(R.styleable.HikLabelEditText_labelContentWeight, 0);
        labelContentHint = array.getString(R.styleable.HikLabelEditText_labelContentHint);
        labelContentHintColor = array.getColor(R.styleable.HikLabelEditText_labelContentHintColor, Color.parseColor("#999999"));
        array.recycle();

        inflate();
    }

    private void inflate() {
        inflate(getContext(), R.layout.hik_view_label_edittext, this);
        label = findViewById(R.id.tv_label);
        labelContext = findViewById(R.id.tv_text);

        if (notnull) {
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder(labelStr + "*");
            ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#FF0000"));
            stringBuilder.setSpan(span, stringBuilder.length() - 1, stringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            label.setText(stringBuilder);
        } else {
            label.setText(labelStr);
        }
        label.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize);
        label.setTextColor(labelColor);
        LayoutParams params1 = (LayoutParams) label.getLayoutParams();
        if (labelGravity == 0) {
            params1.gravity = Gravity.CENTER_VERTICAL;
        }
        if (labelWeight > 0) {
            params1.weight = labelWeight;
        }
        label.setLayoutParams(params1);

        labelContext.setHint(labelContentHint);
        labelContext.setHintTextColor(labelContentHintColor);
        labelContext.setTextColor(labelContextColor);
        labelContext.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelContextSize);
        LayoutParams params2 = (LayoutParams) labelContext.getLayoutParams();
        if (labelContentWeight > 0) {
            params2.weight = labelContentWeight;
        }
        labelContext.setLayoutParams(params2);
    }

    public EditText getLabelContext() {
        return labelContext;
    }
}
